<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Akun extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('video_model','akun_model','kategori_model'));    
		$this->load->library(array('form_validation','session'));    
		$this->load->database();    
		$this->load->helper(array('url','form','string')); 
		//if($this->session->userdata('isLogin') == FALSE) {redirect('index.php/home');}	
		
	}
	
	public function daftar()
	{	
		if($this->session->userdata('isLogin') == true ){
			redirect('index.php/home', refresh);
		} else {
			$data['kategori'] = $this->kategori_model->get_list_kategori();
			$data['daerah'] = $this->kategori_model->get_list_daerah();
			$data['video'] = $this->video_model->get_list_video();
			$data['berhasil'] = true;
			$this->load->view('daftar_v', $data);
		}
	}
	
	public function utama()
	{	
		$data['kategori'] = $this->kategori_model->get_list_kategori();
		$data['daerah'] = $this->kategori_model->get_list_daerah();
		$data['video'] = $this->video_model->get_list_video();
		$pesan['berhasil'] = true;
		$this->load->view('halaman_utama', $pesan);
	}
	
	public function daftar_akun()
	{		
			
			$this->form_validation->set_rules('username', 'Nama Akun', 'trim|required|alpha_dash|is_unique[user.username]|min_length[4]|xss_clean');
			$this->form_validation->set_rules('email', 'Alamat Email', 'trim|required|is_unique[user.email]|valid_email');
			$this->form_validation->set_rules('password', 'Kata Kunci', 'trim|required|min_length[6]|max_length[32]');
			$this->form_validation->set_rules('konfirmpassword', 'Konfirmasi Kata Kunci', 'trim|required|matches[password]');
			
			if($this->form_validation->run() == FALSE)
			{
				$this->daftar();
			}
			else
			{	
				$pass = $this->input->post('password');
				$username = $this->input->post('username');
				$password = md5($pass.$username.'100%ASL1@culturevid2014');
				$data = array(
					'username' => $username,
					'nama_lengkap' => $this->input->post('namalengkap'),
					'password' => $password,
					'jk' => $this->input->post('jk'),
					'tgl_lahir' => $this->input->post('tanggallahir'),
					'email' => $this->input->post('email'),
					'id_peran' => $this->input->post('peran'),
					'pendidikan' => $this->input->post('pendidikan'),
					'pekerjaan' => $this->input->post('pekerjaan'),
					'nama_foto' => 'new.jpg'
				);	
				$username = $this->input->post('username');
				$this->akun_model->insert_user($data);
				$this->session->set_userdata('isLogin', TRUE);
				$this->session->set_userdata('username',$username);
				$status = $this->akun_model->get_status($username);
				$data['akun'] = $this->akun_model->get_data_akun($username);
				$id_peran = $data['akun']->id_peran;
				$id_user = $data['akun']->id_user;
				$session = $this->session->set_userdata('isLogin', TRUE);
				$session = $this->session->set_userdata('peran', $id_peran);
				$session = $this->session->set_userdata('id_user', $id_user);
				//$this->utama();
				$this->verifikasi_email($id_user, $data['email']);
				$this->session->set_flashdata('daftar_sukses', time());
				redirect('index.php/home');
			}
	 }
	
	public function email_aktif($id_user, $kode_enkripsi)
	{	
		$user = $this->akun_model->get_data_user($id_user);
		$email_user = $user['email_user'];
		$enkripsi = sha1($email_user.$id_user.'100%ASL1@culturevid2014');
		if($kode_enkripsi == $enkripsi)
		{	
			$data = array(
					'status_email' => '1'
				);
			$this->akun_model->update_status_email($id_user, $data);
			return true;
			redirect('index.php/home');
		} else {
			echo 'error';
			return false;
			
		}
		
	}
	
	public function verifikasi_email($id_user, $email_user)
	{
		$config = Array(
		  'protocol' => 'smtp',
		  'smtp_host' => 'ssl://smtp.googlemail.com',
		  'smtp_port' => 465,
		  'smtp_user' => 'culturevid@gmail.com', 
		  'smtp_pass' => 'indonesia@raya#1', 
		  'mailtype' => 'html',
		  'charset' => 'iso-8859-1',
		  'wordwrap' => TRUE
		);
		
		$link_aktivasi = base_url().'index.php/akun/email_aktif/'.$id_user.'/'.sha1($email_user.$id_user.'100%ASL1@culturevid2014');
		$message = 'Silahkan aktifasi akun Anda dengan menekan link berikut ini : '.$link_aktivasi;
		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");
		$this->email->from('culturevid@gmail.com'); 
		$this->email->to($email_user);
		$this->email->subject('Aktifasi Pengguna Culture-vid');
		$this->email->message($message);
		if($this->email->send())
		{
			echo 'Email Verifikasi Berhasil Terkirim!';
		}
			else
		{
			show_error($this->email->print_debugger());
		}

	}
	 
	 
	 public function login()
	{
		$this->form_validation->set_rules('username', 'Nama Akun', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'Kata Kunci', 'trim|required|xss_clean');
		$halaman = $this->input->post('hidden');
		$pesan['berhasil'] = TRUE;
		if($this->form_validation->run()==FALSE)
		{	
			redirect('index.php/home');
		}
		  else
		{
			$pesan['berhasil'] = TRUE;
			$username = $this->input->post('username');
			$pass = $this->input->post('password');
			$password = md5($pass.$username.'100%ASL1@culturevid2014');
			$cek = $this->akun_model->get_user($username, $password);
			if($cek <> 0)
			{
				$data['akun'] = $this->akun_model->get_data_akun($username);
				$id_peran = $data['akun']->id_peran;
				$id_user = $data['akun']->id_user;
				$status = $data['akun']->status_user;
				$pesan['berhasil'] = TRUE;
				if($id_peran == 0 || $id_peran == 1 || $id_peran == 2)
				{
					$this->session->set_userdata('isLogin', TRUE);
					$this->session->set_userdata('username',$username);  
					$this->session->set_userdata('peran',$id_peran);  
					$this->session->set_userdata('pass',$password);
					$this->session->set_userdata('id_user',$id_user);
					$this->session->set_userdata('status', $status);
					redirect('index.php/admin');
				}
				else
				{	
					$this->session->set_flashdata('login_sukses', time());
					$this->session->set_userdata('isLogin', TRUE);
					$this->session->set_userdata('username',$username);  
					$this->session->set_userdata('peran', $id_peran);
					$this->session->set_userdata('pass',$password);
					$this->session->set_userdata('status', $status);
					$this->session->set_userdata('id_user', $id_user);
					$this->index();
				}
			}
			else
			{	
				$this->session->set_flashdata('login_gagal', time());
				if($halaman == 'utama'){
					redirect('index.php/home'); 
				} elseif($halaman == 'daftar') 
				{
					$this->session->set_flashdata('login_gagal', time());
					$this->daftar();
				} else 
				{
					
				}
				
			}
		}  
	}
	
	public function keluar_akun()
	{
		$this->session->sess_destroy();
		redirect('index.php/home');
	}
	
	public function index()
	{	
		//$this->output->cache(1);
		$this->cekLog();
		$username = $this->session->userdata('username');
		redirect('index.php/home');
	}
	
	public function profil()
	{
		$this->cekLog();
		$this->cekLog();
		$username = $this->session->userdata('username');
		$data['akun'] = $this->akun_model->get_data_akun($username);
		$data['kategori'] = $this->kategori_model->get_list_kategori();
		$data['daerah'] = $this->kategori_model->get_list_daerah();
		$data['video'] = $this->video_model->get_list_video();
		$id_user = $this->session->userdata('id_user');
		$data['histori_video'] = $this->video_model->get_video_user($id_user);
		$this->load->view('halaman_akun', $data);
	}
	
	public function cekLog()
	{
		if(!$this->session->userdata('isLogin')) { redirect('index.php/home');}
	}
	
	public function ubah_profil()
	{
		$this->cekLog();
		$this->form_validation->set_rules('namaakun', 'Nama Akun', 'trim|required|alpha_dash|min_length[4]|xss_clean');
		$this->form_validation->set_rules('email', 'Alamat Email', 'trim|required|valid_email');
		
		if($this->form_validation->run() == FALSE)
		{
			$this->index();
		}
		else
		{	
			$username = $this->session->userdata('username');
			$id_user = $this->session->userdata('id_user');
			$data = array(
						'username' => $this->input->post('namaakun'),
						'nama_lengkap' => $this->input->post('namalengkap'),
						'jk' => $this->input->post('jk'),
						'tgl_lahir' => $this->input->post('tanggallahir'),
						'email' => $this->input->post('email'),
						'bidang_studi' => $this->input->post('bidang_studi'),
						'pendidikan' => $this->input->post('pendidikan'),
						'pekerjaan' => $this->input->post('pekerjaan')
					);
			$this->akun_model->edit_akun($id_user, $data);				
			redirect('index.php/akun');
		}
	}
	
	public function update_katakunci()
	{	
		$this->cekLog();
		$lama = md5($this->input->post('lama'));
		$id_user = $this->session->userdata('id_user');
		$this->form_validation->set_rules('baru', 'Kata Kunci', 'trim|required|min_length[6]|max_length[32]');
		$this->form_validation->set_rules('konfirm', 'Konfirmasi Kata Kunci', 'trim|required|matches[baru]');
		$this->form_validation->set_rules('lama', 'Password Lama', 'callback_cekpass');
		if($this->form_validation->run() == TRUE) {
			$data = array (
						'password' => md5($this->input->post('baru'))
						);
			$this->akun_model->update_katakunci($id_user, $data);
			$this->index();
		} else {
			$this->index();
		}
	}
	
	public function cekpass($cek)
	{	
		$this->cekLog();
		$lama = md5($this->input->post('lama'));
		$id_user = $this->session->userdata('id_user');
		$cek = $this->akun_model->get_pass($id_user, $lama);
		if($cek <> 0) {
			return TRUE;
		} else { $this->form_validation->set_message('cekpass', 'Password saat ini yang anda masukkan salah!');
			return FALSE;}
	}
	
	
}
